package beautiful.butterfly.schedule.task_manager;

import beautiful.butterfly.schedule.Addresss;
import beautiful.butterfly.schedule.Constants;
import beautiful.butterfly.schedule.Schedules;
import beautiful.butterfly.schedule.Uuids;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TaskHandlers {

    public static TaskHandler createTaskHandler(//
                                                IDataManager dataManager, //
                                                String baseTaskType,//
                                                String ownSign,//
                                                int threadNum)//
            throws Exception {
        TaskHandler taskHandler = new TaskHandler();
        taskHandler.baseTaskType = baseTaskType;
        taskHandler.ownSign = ownSign;
        taskHandler.taskType = Schedules.getTaskType(baseTaskType, ownSign);//baseTaskType$ownSign
        taskHandler.ip = Addresss.getLocalHostAddress();
        taskHandler.hostName = Addresss.getLocalHostName();
        taskHandler.registerTime = new Timestamp(dataManager.zooKeeperCurrentTimeMillis());
        taskHandler.threadNum = threadNum;
        taskHandler.heartBeatTime = null;
        taskHandler.dealInfoDesc = "";
        taskHandler.version = 0;
        taskHandler.serverId = taskHandler.ip + Constants.$ + (Uuids.getUuid());//主要是uuid可能有问题
        String yyMMdd = (new SimpleDateFormat("yyMMdd")).format(new Date(dataManager.zooKeeperCurrentTimeMillis()));
        taskHandler.id = Long.parseLong(yyMMdd) * 100000000 + Math.abs(taskHandler.serverId.hashCode() % 100000000);
        return taskHandler;
    }
}
